package com.xxxt.filter;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

public class PcLoginListener implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		JSONObject json = new JSONObject();
		int status = 0;
		String message = "未登录";
		String msg="";
		HttpSession session = request.getSession();
		String account=(String) session.getAttribute("account");
		if(account==null){
			status = 4;// session验证错误			
			msg = "用户未登录或身份信息已过期";
		}else {
			return true;
		}
		json.put("status", status);
		json.put("message", message);
		json.put("msg", msg);
		responseMessage(response, response.getWriter(), json);
		return false;
	}

	public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
			ModelAndView modelAndView) throws Exception {

	}

	public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			Object o, Exception e) throws Exception {

	}
	private void responseMessage(HttpServletResponse response, PrintWriter out, JSONObject jsonObject) {
		response.setContentType("application/json; charset=utf-8");
		out.print(jsonObject);
		out.flush();
		out.close();
	}
}
